package hongcaosp.app.android.video.player;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import hongcaosp.app.android.common.IListView;
import hongcaosp.app.android.common.PresenterListener;
import hongcaosp.app.android.login.UserToken;
import hongcaosp.app.android.modle.DataCallBack;
import hongcaosp.app.android.modle.bean.IUser;
import hongcaosp.app.android.modle.bean.IVideo;
import hongcaosp.app.android.modle.bean.RecommendList;
import hongcaosp.app.android.modle.bean.Video;
import hongcaosp.app.android.modle.impl.UserFansModleImpl;
import hongcaosp.app.android.modle.impl.UserLikeModleImpl;
import hongcaosp.app.android.modle.impl.VideoModleImpl;
import hongcaosp.app.android.modle.mi.UserFansModle;
import hongcaosp.app.android.modle.mi.UserLikeModle;
import hongcaosp.app.android.modle.mi.VideoModle;
import hongcaosp.app.android.video.VideoEvent;
import xlj.lib.android.base.component.BasePresenter;
import xlj.lib.android.base.toastcompat.ToastManager;

public class VideoPresenter extends BasePresenter {
    private IListView<IVideo> videoView;
    private VideoModle videoModle;
    private UserLikeModle userLikeModle;

    private UserFansModle userFansModle;

    public VideoPresenter() {
        videoModle = new VideoModleImpl();
        userLikeModle = new UserLikeModleImpl();
        userFansModle = new UserFansModleImpl();
    }

    public VideoPresenter(IListView<IVideo> videoView) {
        this.videoView = videoView;
        videoModle = new VideoModleImpl();
        userLikeModle = new UserLikeModleImpl();
        userFansModle = new UserFansModleImpl();
    }

    public void freshData() {
        videoModle.recommendVideo(UserToken.getDefault().getToken(), new DataCallBack<RecommendList>() {
            @Override
            public void onGetData(RecommendList data) {
                super.onGetData(data);
                if (data != null && data.getVideos() != null) {
                    List<Video> videos = data.getVideos();
                    List<IVideo> videoList = new ArrayList<>();
                    videoList.addAll(videos);
                    videoView.freshList(videoList,true);
                } else {
                    videoView.freshList(null,true);
                }
            }

            @Override
            public void onFail(int code, String errorMessage) {
                super.onFail(code, errorMessage);
                ToastManager.getInstance().showToast(errorMessage);
                videoView.freshList(null,true);
            }
        });
    }

    public void loadMore() {
        videoModle.recommendVideo(UserToken.getDefault().getToken(), new DataCallBack<RecommendList>() {
            @Override
            public void onGetData(RecommendList data) {
                super.onGetData(data);
                if (data != null && data.getVideos() != null) {
                    List<Video> videos = data.getVideos();
                    List<IVideo> videoList = new ArrayList<>();
                    if(videos != null) {
                        videoList.addAll(videos);
                    }
                    videoView.addList(videoList,true);
                } else {
                    videoView.addList(null,true);
                }
            }

            @Override
            public void onFail(int code, String errorMessage) {
                super.onFail(code, errorMessage);
                ToastManager.getInstance().showToast(errorMessage);
                videoView.addList(null,true);
            }
        });
    }

    public void likeVideo(final IVideo video, final PresenterListener listener) {
        userLikeModle.add(UserToken.getDefault().getToken(), (int) video.getVid(), new DataCallBack() {
            @Override
            public void onGetData(Object data) {
                super.onGetData(data);
                video.setLike(true);
                video.setLikeCounts(video.getLikeCount() + 1);

                listener.onSuccess();
            }

            @Override
            public void onFail(int code, String errorMessage) {
                super.onFail(code, errorMessage);
                listener.onFail(errorMessage);
            }
        });
    }

    public void cancelikeVideo(final IVideo video, final PresenterListener listener) {
        userLikeModle.cancel(UserToken.getDefault().getToken(), (int) video.getVid(), new DataCallBack() {
            @Override
            public void onGetData(Object data) {
                super.onGetData(data);
                video.setLike(false);
                video.setLikeCounts(video.getLikeCount() - 1);

                listener.onSuccess();
            }

            @Override
            public void onFail(int code, String errorMessage) {
                super.onFail(code, errorMessage);
                listener.onFail(errorMessage);
            }
        });
    }


    public void followUser(final IUser user,final PresenterListener listener) {
        userFansModle.add(UserToken.getDefault().getToken(), (int) user.getUserId(), new DataCallBack() {
            @Override
            public void onGetData(Object data) {
                super.onGetData(data);
                user.setMeFollowTa(true);
                listener.onSuccess();
                EventBus.getDefault().post(new VideoEvent(1,user));
            }

            @Override
            public void onFail(int code, String errorMessage) {
                super.onFail(code, errorMessage);
                listener.onFail(errorMessage);
            }
        });
    }

    public void cancelFollowUser(final IUser user,final PresenterListener listener) {
        userFansModle.cancel(UserToken.getDefault().getToken(), (int) user.getUserId(), new DataCallBack() {
            @Override
            public void onGetData(Object data) {
                super.onGetData(data);
                user.setMeFollowTa(false);

                listener.onSuccess();

                EventBus.getDefault().post(new VideoEvent(1,user));
            }

            @Override
            public void onFail(int code, String errorMessage) {
                super.onFail(code, errorMessage);
                listener.onFail(errorMessage);
            }
        });
    }

    public void forwordVideo(String content,final IVideo video,final PresenterListener listener) {
        videoModle.videoForward(UserToken.getDefault().getToken(), content, video.getVid(), new DataCallBack() {
            @Override
            public void onGetData(Object data) {
                super.onGetData(data);
                video.setForwardCounts(video.getForwardCounts() + 1);
                if (listener != null) {
                    listener.onSuccess();
                }
                EventBus.getDefault().post(new VideoEvent(2,video));
            }

            @Override
            public void onFail(int code, String errorMessage) {
                super.onFail(code, errorMessage);
                if (listener != null) {
                    listener.onFail(errorMessage);
                }
            }
        });
    }

    public void postVideoPlayCount(int videoId) {
        videoModle.playCount(videoId);
    }
}
